package com.ssm.zhihu_ssm.controller;

import com.ssm.zhihu_ssm.entity.Classify;
import com.ssm.zhihu_ssm.entity.Question;
import com.ssm.zhihu_ssm.service.QuestionService;
import com.ssm.zhihu_ssm.service.UserService;
import com.ssm.zhihu_ssm.util.ReturnMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
public class QuestionController {

    @Autowired
    private QuestionService questionService;
    @Autowired
    private UserService userService;

    @RequestMapping("/insertQuestion")//用户提问题
    public Map<String, Object> insertQuestion(String phone, String title, String content_question, int cid){
        int x = userService.findId(phone);
        questionService.insertQuestion(x, title, content_question, cid);
        Map<String, Object> map = ReturnMap.ReturnMapS("true");
        return map;
    }

    @RequestMapping("/followQuestion")//用户关注问题
    public Map<String, Object> followQuestion(String phone, int question_id, boolean isFollow){
        int x = userService.findId(phone);
        questionService.followQuestion(x, question_id, isFollow);
        Map<String, Object> map = ReturnMap.ReturnMapS("true");
        return map;
    }

    @RequestMapping("/getQuestionByUserId")//获得用户所有问题
    @ResponseBody
    public Map<String, Object> getQuestionByUserId(String phone){
        int x = userService.findId(phone);
        List<Question> questionList = questionService.getQuestionByUserId(x);
        Map<String, Object> map = ReturnMap.ReturnMapS(questionList);
        return map;
    }

    @RequestMapping("/getQuestionByClassifyId")//获得话题id获得的所有问题
    @ResponseBody
    public Map<String, Object> getQuestionByClassifyId(int id){
        List<Question> questionList = questionService.getQuestionByClassifyId(id);
        Map<String, Object> map = ReturnMap.ReturnMapS(questionList);
        return map;
    }

//    @RequestMapping("/getQuestionByAllClassify")//获得话题id获得的所有问题
//    @ResponseBody
//    public Map<String, Object> getAllQuestionByClassifyId(){
//        List<Question> questionList = questionService.getQuestionByAllClassify();
//        Map<String, Object> map = ReturnMap.ReturnMapS(questionList);
//        return map;
//    }

    @RequestMapping("/getQuestionByQuestionId")//根据问题id得到问题信息
    @ResponseBody
    public Map<String, Object> getQuestionByQuestionId(int id, String phone){
        int x = userService.findId(phone);
        Question question = questionService.getQuestionByQuestionId(id,x);
        Map<String, Object> map = ReturnMap.ReturnMapS(question);
        return map;
    }

    @RequestMapping("/getClassifyNameByQuestionId")//根据问题id得到问题分类名
    @ResponseBody
    public Map<String, Object> getClassifyNameByQuestionId(int id){
        Map<String,Object> classifyList= questionService.getClassifyNameByQuestionId(id);
        Map<String, Object> map = ReturnMap.ReturnMapS(classifyList);
        return map;
    }

    @RequestMapping("/getClassify")//获得用户所有问题
    @ResponseBody
    public Map<String, Object> getClassify(){
        List<Classify> classifyList = questionService.getClassify();
        Map<String, Object> map = ReturnMap.ReturnMapS(classifyList);
        return map;
    }

    @RequestMapping("/addQuestionSkimNum")//获得用户所有问题
    public Map<String, Object> addQuestionSkimNum(String phone){
        int x = userService.findId(phone);
        questionService.addQuestionSkimNum(x);
        Map<String, Object> map = ReturnMap.ReturnMapS("true");
        return map;
    }

}
